package com.mmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.service.IProductService;
import com.mmall.vo.ProductDetailVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 前端商品查询等控制层
 * Created by qezhhnjy on 2017/9/30-17:45.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService iProductService;

    /**
     * 前端查询指定id商品.不需要验证登录.
     *
     * @param productId 商品id
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping("detail.do")
    public ServerResponse<ProductDetailVo> detail(Integer productId) {
        return iProductService.getDetail(productId);
    }


    /**
     * 前端模糊搜索及品类子商品搜索返回商品列表,并按照价格排序
     *
     * @param keyword    商品名关键字
     * @param categoryId 品类id
     * @param pageNum    页码
     * @param pageSize   每页个数
     * @param orderBy    排序语句("price desc"/"price asc")
     * @return 响应对象
     */
    public ServerResponse<PageInfo> list(@RequestParam(value = "keyword", required = false) String keyword,
                                         @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "orderBy", required = false) String orderBy) {
        return iProductService.getProductByKeywordCategory(keyword, categoryId, pageNum, pageSize, orderBy);
    }

}
